//
//  VistorView.swift
//  01-WeiBo
//
//  Created by 李晓东 on 17/6/25.
//  Copyright © 2017年 PeanutXu. All rights reserved.
//

import UIKit
import SnapKit

protocol VistorViewDelegate {
    func visitorViewDidRegister()
    func visitorViewDidLogin()
}
class VistorView: UIView {
    
    var delegate : VistorViewDelegate?
    
    @objc func registerClick(){
        delegate?.visitorViewDidRegister()
    }
    
    @objc func loginClick(){
        delegate?.visitorViewDidLogin()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupInfo(imageName:String?, tipText:String)  {
        messageLabel.text = tipText
        guard let imgName = imageName else {
            roteImage()
            return
        }
        iconView.image = UIImage(named: imgName)
        homeIconView.isHidden = true
        sendSubview(toBack: maskIconView)
    }
    
    func roteImage(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.isRemovedOnCompletion = false
        iconView.layer.add(anim, forKey: nil)
        
    }
    
    fileprivate lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy var maskIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    fileprivate lazy var homeIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 提示信息 Label
    
    fileprivate lazy var messageLabel: UILabel = UILabel(labelTxt: "你都还没登录,我怎么知道你关注的是谁呢? 你都还没登录,我怎么知道你关注的是谁呢?")
        
//        {
//        let label = UILabel()
//        label.text = "你都还没登录,我怎么知道你关注的是谁呢? 你都还没登录,我怎么知道你关注的是谁呢?"
//        label.font = UIFont.systemFont(ofSize: 14)
//        label.textColor = UIColor.darkGray
//        label.numberOfLines = 0
//        return label
//    }()
    
    /// 注册按钮
    lazy var registerButton: UIButton = UIButton(titleName: "注册", color: UIColor.orange, imageName: "common_button_white_disable")
    
//        {
//        let button = UIButton()
//        button.setTitle("注册", for: .normal)
////        button.setTitleColor(UIColor.orange, for: .normal)
////        button.sizeToFit()
////        button.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), for: .normal)
//        return button
//    }()
    
    /// 登录按钮
    lazy var loginButton: UIButton = UIButton(titleName: "登录", color: UIColor.orange, imageName: "common_button_white_disable")
        
//    {
//        let button = UIButton()
//        button.setTitle(" 登录", for: .normal)
//        button.setTitleColor(UIColor.orange, for: .normal)
//        button.sizeToFit()
//        button.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), for: .normal)
//        return button
//    }()
}

extension VistorView{
    
    @objc func register2Click(){
        
    }
    
    fileprivate func setupUI(){
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
//        addConstraint(NSLayoutConstraint.init(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint.init(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0))
        
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(44)
        }
        
//        addConstraint(NSLayoutConstraint.init(item: homeIconView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint.init(item: homeIconView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1.0, constant: 0))
        
        homeIconView.snp.makeConstraints { (li) in
            li.centerX.equalTo(iconView.snp.centerX)
            li.centerY.equalTo(iconView.snp.centerY)
        }
        
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1.0, constant: 16))
        
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 224))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        messageLabel.snp.makeConstraints { (xiao) in
            xiao.centerX.equalTo(iconView.snp.centerX)
            xiao.top.equalTo(iconView.snp.bottom)
            xiao.width.equalTo(224)
            xiao.height.equalTo(36)
        }
        messageLabel.textAlignment = .center
        
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: messageLabel, attribute: .left, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 90))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        registerButton.snp.makeConstraints { (dong) in
            dong.left.equalTo(messageLabel.snp.left)
            dong.top.equalTo(messageLabel.snp.bottom).offset(16)
            dong.width.equalTo(90)
            dong.height.equalTo(36)
        }
        
        //
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: messageLabel, attribute: .right, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 90))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        loginButton.snp.makeConstraints { (dong) in
            dong.right.equalTo(messageLabel.snp.right)
            dong.top.equalTo(messageLabel.snp.bottom).offset(16)
            dong.width.equalTo(90)
            dong.height.equalTo(36)
        }
        
//        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskIconView]))
//        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[mask]-(btnHeight)-[registerBtn]", options: [], metrics: ["btnHeight":-36], views: ["mask":maskIconView, "registerBtn":registerButton]))
//        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        maskIconView.snp.makeConstraints { (dong) in
            dong.top.equalTo(self.snp.top)
            dong.right.equalTo(self.snp.right)
            dong.left.equalTo(self.snp.left)
            dong.bottom.equalTo(registerButton.snp.bottom).offset(-36)
        }
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        registerButton.addTarget(self, action: #selector(VistorView.registerClick), for: .touchUpInside)
        loginButton.addTarget(self, action: #selector(VistorView.loginClick), for: .touchUpInside)
    }
}
